package com.hisense.ovcloud.mqtt.monitor;

import com.hisense.ovcloud.mqtt.monitor.service.MQTTClientConnector;
import com.hisense.ovcloud.mqtt.monitor.service.MqttClients;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.web.reactive.config.EnableWebFlux;


@SpringBootApplication(scanBasePackages={
        "com.hisense.ovcloud.commons",
        "com.hisense.ovcloud.mqtt.monitor",
})
@EnableWebFlux
@EnableAsync
@ComponentScan(basePackages = {
        "com.hisense.ovcloud.commons",
        "com.hisense.ovcloud.mqtt.monitor"})
@Slf4j
public class Main implements CommandLineRunner {

    private final MQTTClientConnector mqttClientConnector;

    private final MqttClients mqttClients;

    public Main(MqttClients mqttClients, MQTTClientConnector mqttClientConnector) {
        this.mqttClients = mqttClients;
        this.mqttClientConnector = mqttClientConnector;
    }

    public static void main(String[] args) {
        SpringApplication.run(Main.class, args);
    }

    @Override
    public void run(String... args){
        mqttClients.connectItSelf().subscribe();
        try {
            Thread.sleep( 20 * 1000L);
            log.info("start to check mqtt connection status.");
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }

        mqttClientConnector.reconnect(10);
    }
}
